package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.eqp.entity.MesPallet;
import org.jeecg.eqp.entity.MesPalletHistory;
import org.jeecg.eqp.service.IMesPalletHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Api(tags = "托盘历史数据")
@RestController
@RequestMapping("/eqp/palletHis")
@Slf4j
public class MesPalletHisController extends JeecgController<MesPalletHistory, IMesPalletHistoryService> {

    @Autowired
    private IMesPalletHistoryService mesPalletHistoryService;

    @ApiOperation(value = "托盘历史数据-分页列表查询", notes = "托盘历史数据-分页列表查询")
    @GetMapping(value = "/hisList")
    @DS("#header.line")
    public Result<IPage<MesPalletHistory>> queryPageHisList(MesPalletHistory his,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<MesPalletHistory> queryWrapper = QueryGenerator.initQueryWrapper(his, req.getParameterMap());
        queryWrapper.orderByDesc("ctime");
        Page<MesPalletHistory> page = new Page<MesPalletHistory>(pageNo, pageSize);
        IPage<MesPalletHistory> pageList = mesPalletHistoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 通过id查询托盘历史数据
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "托盘历史数据-通过id查询")
    @ApiOperation(value = "托盘历史数据-通过id查询", notes = "托盘历史数据-通过id查询")
    @GetMapping(value = "/queryByHisId")
    @DS("#header.line")
    public Result<MesPalletHistory> queryByHisId(@RequestParam(name = "id", required = true) String id) {
        MesPalletHistory his = mesPalletHistoryService.getById(id);
        if (his == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(his);
    }

    /**
     * 编辑
     *
     * @param eqpEqpchkCfg
     * @return
     */
    @AutoLog(value = "托盘历史数据-编辑")
    @ApiOperation(value = "托盘历史数据-编辑", notes = "托盘历史数据-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody MesPalletHistory mesPallet) throws Exception {
        //框架修改 eqpEqpchkCfgService.updateById(eqpEqpchkCfg);
        //return Result.OK("编辑成功!");
        mesPallet.setCtime(new Date());
        mesPalletHistoryService.updateById(mesPallet);
        return Result.OK("修改成功！");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param his
     */
    @RequestMapping(value = "/exportHisXls")
    @DS("#header.line")
    public ModelAndView exportHisXls(HttpServletRequest request, MesPalletHistory his) {
        return super.exportXls(request, his, MesPalletHistory.class, "托盘历史数据");
    }
}
